﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Intelligent.OTC.Business;
using Intelligent.OTC.Domain.DataModel;
using System.Web;
using Intelligent.OTC.Common;
using Intelligent.OTC.Common.Attr;
using System.Configuration;
using System.IO;
namespace Intelligent.OTC.WebApi.Controllers
{
    [UserAuthorizeFilter(actionSet: "soa")]
    public class GenerateSOAController : ApiController
    {
        public const string strMailAttachmentPath = "MailAttachmentPath";//GenerateSOA路径的config保存名
        public const string strArchiveMailAttachmentPath = "ArchiveMailAttachmentPath";//ArchiveSOA路径的config保存名

        //[HttpPost]
        //[Route("api/generateSOA/generateTemp")]
        //public IList<string> Post(int Type, [FromBody]List<int> intIds)
        //{
        //    List<InvoiceAging> invoicelist = new List<InvoiceAging>();
        //    //List<int> intids = new List<int>();
        //    InvoiceService service = SpringFactory.GetObjectImpl<InvoiceService>("InvoiceService");

        //    //foreach (var item in strids)
        //    //{
        //    //    //InvoiceAging invoice = service.CommonRep.FindBy<InvoiceAging>(Convert.ToInt32(item));
        //    //    InvoiceAging invoice = service.CommonRep.GetQueryable<InvoiceAging>().Where(inv => inv.InvoiceNum == item).FirstOrDefault();
        //    //    invoicelist.Add(invoice);
        //    //   // intids.Add(Convert.ToInt32(item));
        //    //    intids.Add(item);
        //    //}

        //    List<string> lstPath = service.setContent(intIds, Type);


        //    return lstPath;
        //}

        [HttpPost]
        public void Post([FromBody]MailTmp mail)
        {
            //IMailService service = SpringFactory.GetObjectImpl<IMailService>("MailService");
            // mail.Deal = AppContext.Current.User.Deal;
            // mail.MessageId = Guid.NewGuid().ToString();
            // mail.Type = "OUT";
            // mail.CreateTime = AppContext.Current.User.Now;
            // mail.Operator = AppContext.Current.User.EID;
            // if (IsMail == "true")
            // {
            // service.SendMail(mail);
            // }
            // //added by zhangYu
            // List<Mail> mailList = new List<Mail>();
            // mailList.Add(mail);
            // service.saveSendMail(mailList);

            // //contact_his, invoice_log
            // string[] cus = mail.Bussiness_Reference.Split(',');
            ISoaService soaservice = SpringFactory.GetObjectImpl<ISoaService>("SoaService");
            // soaservice.AddContactHistory("Mail", cus, mail);
            //if ("1".Equals(mail.soaFlg))//SQA
            // {
            //     soaservice.AddInvoiceLog(mail);
            // }
            MailService mailService = SpringFactory.GetObjectImpl<MailService>("MailService");

            soaservice.sendSoaSaveInfoToDB(mail);
        }

        [HttpPost]
        [Route("api/generateSOA/generate")]
        public MailTmp GetSOAMailInstance(string customerNums, [FromBody]List<int> intIds)
        {
            ISoaService service = SpringFactory.GetObjectImpl<ISoaService>("SoaService");
            return service.GetNewMailInstance(customerNums, intIds);
        }

        [HttpPost]
        [Route("api/generateSOA/generateTemp")]
        public MailTmp GetSOAMailInstance(string customerNums, int templateId, [FromBody]List<int> intIds)
        {
            ISoaService service = SpringFactory.GetObjectImpl<ISoaService>("SoaService");
            return service.GetNewMailInstance(customerNums, intIds, templateId);
        }


        [HttpPost]
        [Route("api/generateSOA/generateAtta")]
        public IList<string> Post(int Type, [FromBody]List<int> intIds)
        {
            List<InvoiceAging> invoicelist = new List<InvoiceAging>();
            
            InvoiceService service = SpringFactory.GetObjectImpl<InvoiceService>("InvoiceService");

            List<SOAInfoReportItem> reportItemList = new List<SOAInfoReportItem>();
            List<string> lstPath = service.setContent(intIds, Type, out reportItemList);
            return lstPath;
        }
    }
}